<?php
/*
 * Name: pholioio
 * Author: Bryan Smith
 * Date: 2012/02/10
 * Description: This application presents a form to a web user
 * and takes the input and generates an SVG with the input
 * embedded within it.
 * Future: This application aims to be an SVG template engine
 * 
 */


$font = './includes/fonts/Verdana.ttf';
$font_size = 12;
	


header('Content-type: image/svg+xml');

$file = './includes/templates/1/tcm-portfolio.svg';
$fp = fopen($file, 'r');
$xml = fread($fp, filesize($file));

$svg = new SimpleXMLElement($xml);

$g = $svg->g;

{ // Header section
$header_style = 'font-size:9.6px;text-align:end;text-anchor:end;fill:#a8292f;fill-opacity:1;font-family:Garuda;';
$group = $svg->addChild('g');
$group->addAttribute('id', 'header');

$tempvar = $group->addChild('text', 'maker');
$tempvar->addAttribute('style', $header_style);
$tempvar->addAttribute('x', '125');
$tempvar->addAttribute('y', '420');

$tempvar = $group->addChild('text', 'project title');
$tempvar->addAttribute('style', $header_style);
$tempvar->addAttribute('x', '125');
$tempvar->addAttribute('y', '440');

$tempvar = $group->addChild('text', 'materials used');
$tempvar->addAttribute('style', $header_style);
$tempvar->addAttribute('x', '125');
$tempvar->addAttribute('y', '460');

$tempvar = $group->addChild('text', 'process &amp; function');
$tempvar->addAttribute('style', $header_style);
$tempvar->addAttribute('x', '125');
$tempvar->addAttribute('y', '490');
}

{ // Input fields section
$input_style = 'font-size:9.6px;fill:#231f20;fill-opacity:1;font-family:Verdana';
$group = $svg->addChild('g');
$group->addAttribute('id', 'input');

$tempvar = $group->addChild('text', 'Christopher Odegard');
$tempvar->addAttribute('style', $input_style);
$tempvar->addAttribute('x', '135');
$tempvar->addAttribute('y', '420');

$tempvar = $group->addChild('text', 'Tantalus — carrying case for spirits tasting glassware');
$tempvar->addAttribute('style', $input_style);
$tempvar->addAttribute('x', '135');
$tempvar->addAttribute('y', '440');

$wood_text = 'woods: cherry, cherry burl, walnut, spanish cedar, and wenge; off-the-rack ' . 
	'and custom brass hardware; french-fit foam lined with synthetic black velvet';

$text_arr = breakdown($wood_text, 600, $font, 9.6);

$y = 460;

foreach($text_arr as $line) {
	$text = implode(' ', $line);
	$tempvar = $group->addChild('text', $text);
	
	$tempvar->addAttribute('style', $input_style);
	$tempvar->addAttribute('x', '135');
	$tempvar->addAttribute('y', $y);
	
	$y += 10;
}

$y += 10;

$fill_text = 'Quadrupei pessimus libere senesceret pretosius umbraculi, iam apparatus bellis vocificat optimus perspicax ' . 
	'zothecas, et verecundus concubine frugaliter senesceret umbraculi. Perspicax saburre neglegenter praemuniet bellus ' . 
	'cathedras. Ossifragi vocificat Octavius. Caesar suffragarit adlaudabilis fiducias. Augustus agnascor adfabilis ' . 
	'agricolae, ut cathedras pessimus comiter conubium santet umbraculi, semper pretosius agricolae vocificat adfabilis ' . 
	'quadrupei, et optimus lascivius. Rures praemuniet saburre.';

$text_arr = breakdown($fill_text, 600, $font, 9.6);

foreach($text_arr as $line) {
	$text = implode(' ', $line);
	$tempvar = $group->addChild('text', $text);
	
	$tempvar->addAttribute('style', $input_style);
	$tempvar->addAttribute('x', '135');
	$tempvar->addAttribute('y', $y);
	
	$y += 10;
}



}

{ // Biography section
$bio_style = 'font-size:7.5px;font-weight:300;fill:#231f20;fill-opacity:1;font-family:CorporateS;';
$group = $svg->addChild('g');
$group->addAttribute('id', 'maker-bio');

// Need to break this up into lines
$bio_text = 'Christopher Odegard is a graphic designer by trade and an amateur wood craftsman by inclination. He ' . 
	'currently serves as interim department head for the wood shop and CNC areas, and has been a TC Maker member ' . 
	'since August 2011. His personal interests include: being a father, fine woodworking with emphasis on hand-tools ' . 
	'and traditional craft techniques,fine spirits &amp; good food, audio electronics, geocaching, hand-drums, as ' . 
	'well as fine art, music and film appreciation.';

$text_arr = breakdown($bio_text, 575, $font, 7.5);

$y = 680;

foreach($text_arr as $line) {
	$text = implode(' ', $line);
	$tempvar = $group->addChild('text', $text);
	
	$tempvar->addAttribute('style', $bio_style);
	$tempvar->addAttribute('x', '160');
	$tempvar->addAttribute('y', $y);
	
	$y += 10;
}


}

echo $svg->asXML();

// Adding an image
/*
$image = $group->addChild('image');
$image->addAttribute('x', '100');
$image->addAttribute('y', '100');
$image->addAttribute('height', '1');
$image->addAttribute('width', '1');
$image->addAttribute('xlink:href', '');
*/

function breakdown($text, $width, $font, $font_size){
	$text_words = explode(" ", $text);  // Split input into array or words
	$rev_words = array_reverse($text_words);  // Reverse the array of words to use as a stack
	
	$lines[] = array(); // An array of arrays of words; 1st index: line number
	$num_line = 0; // Starting line index of 2D array

	do
	{
	  $line = '';
	
	  $word = array_pop($rev_words);  //Get first word
	  array_push($lines[$num_line], $word);  // Push the word to the $lines array
	
	  $line .= implode(' ', $lines[$num_line]); // Convert the $lines[$num_line] array to a string
	  $line_len = getLength($line, $font, $font_size); // Get TTF Bounding Box length of string
	
	  if ($line_len > $width)  // Get rendered length of line
	  {
	    array_pop($lines[$num_line]);
	    array_push($rev_words, $word);
	    $num_line += 1;
	    $lines[] = array();
	  }
	} while (count($rev_words) > 0);
	
	return $lines;
}


function getLength($candidate, $font, $font_size){
  $bbox = imagettfbbox($font_size, 0, $font, $candidate);
  return ($bbox[2] - $bbox[0]);
}

?>